package ru.ksu.niimm.cll.transword.server.services;

import org.springframework.beans.factory.annotation.Autowired;

import ru.ksu.niimm.cll.transword.server.dao.FixedExpressionDAO;
import ru.ksu.niimm.cll.transword.shared.dto.FixedExpressionDTO;
import ru.ksu.niimm.cll.transword.shared.services.FixedExpressionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; 

@Service("fixedExpressionService")
public class FixedExpressionServiceImpl implements FixedExpressionService {
	
	@Autowired
	private FixedExpressionDAO fixedExpressionDAO;

	@PostConstruct
	public void init() throws Exception {
	}
	
	@PreDestroy
	public void destroy() {
	}

	
	public FixedExpressionDTO findFixedExpression(int fixedExpressionId) {
		return fixedExpressionDAO.findById(new Long(fixedExpressionId));
	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)

	public void saveFixedExpression(int fixedExpressionId, String name,
			String description, String usageStyle) throws Exception {
        FixedExpressionDTO fixedExpressionDTO = fixedExpressionDAO.findById(new Long(fixedExpressionId));
		
		if(fixedExpressionDTO == null) {
			fixedExpressionDTO = new FixedExpressionDTO(fixedExpressionId, name, description, usageStyle);
			fixedExpressionDAO.persist(fixedExpressionDTO);
		}
		
	}
	
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void updateFixedEXpression(int fixedExpressionId, String name,
			String description, String usageStyle) throws Exception {
		
       FixedExpressionDTO feDTO = fixedExpressionDAO.findById(new Long(fixedExpressionId));
		
		if(feDTO != null) {
			feDTO.setFixedExpressionName(name);
			feDTO.setFixedExpressionId(fixedExpressionId);
			feDTO.setFixedExpressionDescription(description);
			feDTO.setUsageStyle(usageStyle);
		}
	}
	
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void saveOrUpdateFixedExpression(int fixedExpressionId,
			String name, String description, String usageStyle) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteFixedExpression(int fixedExpressionId) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
